package com.estrend.redbird.extend

import android.view.View
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.*

//Flow点击事件防抖
fun View.setOnDebouncedClickListener(
    debounceTime: Long = 300L,
    coroutineScope: CoroutineScope,
    onClick: (View) -> Unit
) {
    val debounceFlow = callbackFlow {
        setOnClickListener {
            trySend(it)
        }
        awaitClose { setOnClickListener(null) }
    }.debounce(debounceTime)

    coroutineScope.launch {
        debounceFlow.collect {
            onClick(it)
        }
    }
}

fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }


